<template><div><h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<h2 id="环境要求" tabindex="-1"><a class="header-anchor" href="#环境要求"><span>环境要求</span></a></h2>
<p>PHP &gt;= 5.6.0</p>
<h2 id="使用composer安装" tabindex="-1"><a class="header-anchor" href="#使用composer安装"><span>使用Composer安装</span></a></h2>
<p>如果您没有 <a href="http://getcomposer.org/" target="_blank" rel="noopener noreferrer">Composer</a>，请按照文档的 <a href="https://learnku.com/docs/yii-framework/2.0.x/installation-yii/11954#1dabc3" target="_blank" rel="noopener noreferrer">安装Yii</a> 部分中的说明进行安装。</p>
<p>安装 Composer 后，您可以使用以下命令安装应用程序：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> create-project --prefer-dist yiisoft/yii2-app-advanced advanced</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>此命令将高级应用程序模板安装在名为 <code v-pre>advanced</code> 的目录中。 如果需要，您可以选择不同的目录名称，来表示您自己的项目。</p>
<h2 id="从归档文件安装-不推荐" tabindex="-1"><a class="header-anchor" href="#从归档文件安装-不推荐"><span>从归档文件安装（不推荐）</span></a></h2>
<p>将从 <a href="http://www.yiiframework.com/download/" target="_blank" rel="noopener noreferrer">yiiframework.com</a> 下载的归档文件解压缩到直接位于Web根目录下的名为 <code v-pre>advanced</code> 的目录。然后按照下一小节中给出的说明进行操作。</p>
<h2 id="准备应用程序" tabindex="-1"><a class="header-anchor" href="#准备应用程序"><span>准备应用程序</span></a></h2>
<p>安装应用程序后，必须执行以下步骤来初始化已安装的应用程序。 这些操作仅需执行一次即可。</p>
<ol>
<li>打开控制台终端，执行 <code v-pre>init</code> 命令并选择 <code v-pre>dev</code> 作为环境。</li>
</ol>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token comment"># 命令大纲</span></span>
<span class="line">/path/to/php-bin/php /path/to/yii-application/init</span>
<span class="line"></span>
<span class="line"><span class="token comment"># 例如</span></span>
<span class="line"><span class="token comment"># 进入项目根目录</span></span>
<span class="line"><span class="token builtin class-name">cd</span> advanced</span>
<span class="line"><span class="token comment"># php配置过环境变量</span></span>
<span class="line">php init</span>
<span class="line"><span class="token comment"># 开启交互模式</span></span>
<span class="line"><span class="token number">0</span></span>
<span class="line"><span class="token function">yes</span></span>
<span class="line">all</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果，您使用脚本自动化，可以在非交互模式下执行 <code v-pre>init</code> 。在此环境下，没有用户交互输入命令执行，所以，直接将参数写在命令语句中。</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token comment"># env 参数有 Development 与 Production</span></span>
<span class="line"><span class="token comment"># overwrite 参数有 All 与 No</span></span>
<span class="line">/path/to/php-bin/php /path/to/yii-application/init <span class="token parameter variable">--env</span><span class="token operator">=</span>Production <span class="token parameter variable">--overwrite</span><span class="token operator">=</span>All <span class="token parameter variable">--delete</span><span class="token operator">=</span>All</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><ol start="2">
<li>
<p>创建一个新数据库，并相应地调整 <code v-pre>common/config/main-local.php</code> 中的 <code v-pre>components['db']</code> 配置，可以参考如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line"> <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">     <span class="token string single-quoted-string">'db'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">          <span class="token comment">//默认数据库，或主库</span></span>
<span class="line">         <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\db\Connection'</span><span class="token punctuation">,</span></span>
<span class="line">         <span class="token string single-quoted-string">'dsn'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mysql:host=localhost;port=3306;dbname=demo'</span><span class="token punctuation">,</span></span>
<span class="line">         <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'root'</span><span class="token punctuation">,</span></span>
<span class="line">         <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">''</span><span class="token punctuation">,</span></span>
<span class="line">         <span class="token string single-quoted-string">'charset'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'utf8mb4'</span><span class="token punctuation">,</span><span class="token comment">//常用字符集，且支持emoji</span></span>
<span class="line">         <span class="token string single-quoted-string">'tablePrefix'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'demo_'</span><span class="token punctuation">,</span><span class="token comment">//表前缀</span></span>
<span class="line">         <span class="token string single-quoted-string">'attributes'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">             <span class="token class-name static-context">PDO</span><span class="token operator">::</span><span class="token constant">ATTR_STRINGIFY_FETCHES</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token comment">// 提取数据的时候，数值转是否换为字符串</span></span>
<span class="line">             <span class="token class-name static-context">PDO</span><span class="token operator">::</span><span class="token constant">ATTR_EMULATE_PREPARES</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token comment">// 启用或禁用预处理语句的模拟</span></span>
<span class="line">         <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">         <span class="token comment">// 'enableSchemaCache' => true, // 是否开启缓存, 请了解其中机制在开启，不了解谨慎</span></span>
<span class="line">         <span class="token comment">// 'schemaCacheDuration' => 3600, // 缓存时间</span></span>
<span class="line">         <span class="token comment">//'schemaCacheExclude' => [],//不应该缓存元数据的表的列表。默认为空数组</span></span>
<span class="line"></span>
<span class="line">         <span class="token comment">// 'enableQueryCache' => true, // 是否启用查询缓存</span></span>
<span class="line">         <span class="token comment">// 'queryCacheDuration' => 3600, //查询结果缓存时间</span></span>
<span class="line"></span>
<span class="line">         <span class="token comment">// 一主多从，从库的通用配置</span></span>
<span class="line">         <span class="token comment">/*'slaveConfig' => [</span>
<span class="line">             'username' => 'root',</span>
<span class="line">             'password' => '',</span>
<span class="line">             'charset' => 'utf8mb4',//常用字符集，且支持emoji</span>
<span class="line">             'tablePrefix' => 'demo_',//表前缀</span>
<span class="line">             'attributes' => [</span>
<span class="line">                 PDO::ATTR_STRINGIFY_FETCHES => false, // 提取数据的时候，数值转是否换为字符串</span>
<span class="line">                 PDO::ATTR_EMULATE_PREPARES => false, // 启用或禁用预处理语句的模拟</span>
<span class="line"></span>
<span class="line">                 // 使用一个更小的连接超时</span>
<span class="line">                 PDO::ATTR_TIMEOUT => 10,</span>
<span class="line">             ],</span>
<span class="line">         ],*/</span></span>
<span class="line"></span>
<span class="line">         <span class="token comment">// 从库的配置列表</span></span>
<span class="line">         <span class="token comment">/*'slaves' => [</span>
<span class="line">             ['dsn' => 'mysql:host=localhost;port=3306;dbname=demo1'],</span>
<span class="line">             ['dsn' => 'mysql:host=localhost;port=3306;dbname=demo2'],</span>
<span class="line">         ],*/</span></span>
<span class="line"></span>
<span class="line">         <span class="token comment">/*'on afterOpen' => function($event) {</span>
<span class="line">             // $event->sender refers to the DB connection</span>
<span class="line">             $event->sender->createCommand("SET time_zone = 'UTC'")->execute();</span>
<span class="line">         }*/</span></span>
<span class="line">     <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"> <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></li>
<li>
<p>打开控制台终端，执行数据库迁移命令（项目模板中有自带的示例的迁移脚本，通常在 <code v-pre>/console/migrations/</code> 目录下）：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token comment"># 命令大纲</span></span>
<span class="line">/path/to/php-bin/php /path/to/yii-application/yii migrate</span>
<span class="line"><span class="token comment"># 例如</span></span>
<span class="line"><span class="token comment"># 通常在项目跟目录下执行，即可跟踪显示迁移信息</span></span>
<span class="line">php yii migrate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></li>
<li>
<p>设置 Web 服务器的文档根目录：</p>
<ul>
<li>对于前台 <code v-pre>/path/to/yii-application/frontend/web/</code> 并且使用URL <code v-pre>http://frontend.test/</code></li>
<li>对于后台 <code v-pre>/path/to/yii-application/backend/web/</code> 并且使用URL <code v-pre>http://backend.test/</code></li>
</ul>
</li>
</ol>
<p>对于Apache，使用如下配置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">       <span class="token operator">&lt;</span>VirtualHost <span class="token operator">*</span><span class="token punctuation">:</span><span class="token number">80</span><span class="token operator">></span></span>
<span class="line">           ServerName frontend<span class="token operator">.</span>test</span>
<span class="line">           DocumentRoot <span class="token string double-quoted-string">"/path/to/yii-application/frontend/web/"</span></span>
<span class="line"></span>
<span class="line">           <span class="token operator">&lt;</span>Directory <span class="token string double-quoted-string">"/path/to/yii-application/frontend/web/"</span><span class="token operator">></span></span>
<span class="line">               <span class="token comment"># use mod_rewrite for pretty URL support</span></span>
<span class="line">               RewriteEngine on</span>
<span class="line">               <span class="token comment"># If a directory or a file exists, use the request directly</span></span>
<span class="line">               RewriteCond <span class="token operator">%</span><span class="token punctuation">{</span><span class="token constant">REQUEST_FILENAME</span><span class="token punctuation">}</span> <span class="token operator">!</span><span class="token operator">-</span>f</span>
<span class="line">               RewriteCond <span class="token operator">%</span><span class="token punctuation">{</span><span class="token constant">REQUEST_FILENAME</span><span class="token punctuation">}</span> <span class="token operator">!</span><span class="token operator">-</span>d</span>
<span class="line">               <span class="token comment"># Otherwise forward the request to index.php</span></span>
<span class="line">               RewriteRule <span class="token operator">.</span> index<span class="token operator">.</span>php</span>
<span class="line"></span>
<span class="line">               <span class="token comment"># use index.php as index file</span></span>
<span class="line">               DirectoryIndex index<span class="token operator">.</span>php</span>
<span class="line"></span>
<span class="line">               <span class="token comment"># ...other settings...</span></span>
<span class="line">           <span class="token operator">&lt;</span><span class="token operator">/</span>Directory<span class="token operator">></span></span>
<span class="line">       <span class="token operator">&lt;</span><span class="token operator">/</span>VirtualHost<span class="token operator">></span></span>
<span class="line"></span>
<span class="line">       <span class="token operator">&lt;</span>VirtualHost <span class="token operator">*</span><span class="token punctuation">:</span><span class="token number">80</span><span class="token operator">></span></span>
<span class="line">           ServerName backend<span class="token operator">.</span>test</span>
<span class="line">           DocumentRoot <span class="token string double-quoted-string">"/path/to/yii-application/backend/web/"</span></span>
<span class="line"></span>
<span class="line">           <span class="token operator">&lt;</span>Directory <span class="token string double-quoted-string">"/path/to/yii-application/backend/web/"</span><span class="token operator">></span></span>
<span class="line">               <span class="token comment"># use mod_rewrite for pretty URL support</span></span>
<span class="line">               RewriteEngine on</span>
<span class="line">               <span class="token comment"># If a directory or a file exists, use the request directly</span></span>
<span class="line">               RewriteCond <span class="token operator">%</span><span class="token punctuation">{</span><span class="token constant">REQUEST_FILENAME</span><span class="token punctuation">}</span> <span class="token operator">!</span><span class="token operator">-</span>f</span>
<span class="line">               RewriteCond <span class="token operator">%</span><span class="token punctuation">{</span><span class="token constant">REQUEST_FILENAME</span><span class="token punctuation">}</span> <span class="token operator">!</span><span class="token operator">-</span>d</span>
<span class="line">               <span class="token comment"># Otherwise forward the request to index.php</span></span>
<span class="line">               RewriteRule <span class="token operator">.</span> index<span class="token operator">.</span>php</span>
<span class="line"></span>
<span class="line">               <span class="token comment"># use index.php as index file</span></span>
<span class="line">               DirectoryIndex index<span class="token operator">.</span>php</span>
<span class="line"></span>
<span class="line">               <span class="token comment"># ...other settings...</span></span>
<span class="line">           <span class="token operator">&lt;</span><span class="token operator">/</span>Directory<span class="token operator">></span></span>
<span class="line">       <span class="token operator">&lt;</span><span class="token operator">/</span>VirtualHost<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>nginx使用如下配置：</p>
<div class="language-nginx line-numbers-mode" data-highlighter="prismjs" data-ext="nginx" data-title="nginx"><pre v-pre class="language-nginx"><code><span class="line">       <span class="token directive"><span class="token keyword">server</span></span> <span class="token punctuation">{</span></span>
<span class="line">           <span class="token directive"><span class="token keyword">charset</span> utf-8</span><span class="token punctuation">;</span></span>
<span class="line">           <span class="token directive"><span class="token keyword">client_max_body_size</span> <span class="token number">128M</span></span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">           <span class="token directive"><span class="token keyword">listen</span> <span class="token number">80</span></span><span class="token punctuation">;</span> <span class="token comment">## listen for ipv4</span></span>
<span class="line">           <span class="token comment">#listen [::]:80 default_server ipv6only=on; ## listen for ipv6</span></span>
<span class="line"></span>
<span class="line">           <span class="token directive"><span class="token keyword">server_name</span> frontend.test</span><span class="token punctuation">;</span></span>
<span class="line">           <span class="token directive"><span class="token keyword">root</span>        /path/to/yii-application/frontend/web/</span><span class="token punctuation">;</span></span>
<span class="line">           <span class="token directive"><span class="token keyword">index</span>       index.php</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">           <span class="token directive"><span class="token keyword">access_log</span>  /path/to/yii-application/log/frontend-access.log</span><span class="token punctuation">;</span></span>
<span class="line">           <span class="token directive"><span class="token keyword">error_log</span>   /path/to/yii-application/log/frontend-error.log</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">           <span class="token directive"><span class="token keyword">location</span> /</span> <span class="token punctuation">{</span></span>
<span class="line">               <span class="token comment"># Redirect everything that isn't a real file to index.php</span></span>
<span class="line">               <span class="token directive"><span class="token keyword">try_files</span> <span class="token variable">$uri</span> <span class="token variable">$uri</span>/ /index.php<span class="token variable">$is_args</span><span class="token variable">$args</span></span><span class="token punctuation">;</span></span>
<span class="line">           <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">           <span class="token comment"># uncomment to avoid processing of calls to non-existing static files by Yii</span></span>
<span class="line">           <span class="token comment">#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {</span></span>
<span class="line">           <span class="token comment">#    try_files $uri =404;</span></span>
<span class="line">           <span class="token comment">#}</span></span>
<span class="line">           <span class="token comment">#error_page 404 /404.html;</span></span>
<span class="line"></span>
<span class="line">           <span class="token comment"># deny accessing php files for the /assets directory</span></span>
<span class="line">           <span class="token directive"><span class="token keyword">location</span> ~ ^/assets/.*\.php$</span> <span class="token punctuation">{</span></span>
<span class="line">               <span class="token directive"><span class="token keyword">deny</span> all</span><span class="token punctuation">;</span></span>
<span class="line">           <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">           <span class="token directive"><span class="token keyword">location</span> ~ \.php$</span> <span class="token punctuation">{</span></span>
<span class="line">               <span class="token directive"><span class="token keyword">include</span> fastcgi_params</span><span class="token punctuation">;</span></span>
<span class="line">               <span class="token directive"><span class="token keyword">fastcgi_param</span> SCRIPT_FILENAME <span class="token variable">$document_root</span><span class="token variable">$fastcgi_script_name</span></span><span class="token punctuation">;</span></span>
<span class="line">               <span class="token directive"><span class="token keyword">fastcgi_pass</span> 127.0.0.1:9000</span><span class="token punctuation">;</span></span>
<span class="line">               <span class="token comment">#fastcgi_pass unix:/var/run/php5-fpm.sock;</span></span>
<span class="line">               <span class="token directive"><span class="token keyword">try_files</span> <span class="token variable">$uri</span> =404</span><span class="token punctuation">;</span></span>
<span class="line">           <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">           <span class="token directive"><span class="token keyword">location</span> ~* /\.</span> <span class="token punctuation">{</span></span>
<span class="line">               <span class="token directive"><span class="token keyword">deny</span> all</span><span class="token punctuation">;</span></span>
<span class="line">           <span class="token punctuation">}</span></span>
<span class="line">       <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">       <span class="token directive"><span class="token keyword">server</span></span> <span class="token punctuation">{</span></span>
<span class="line">           <span class="token directive"><span class="token keyword">charset</span> utf-8</span><span class="token punctuation">;</span></span>
<span class="line">           <span class="token directive"><span class="token keyword">client_max_body_size</span> <span class="token number">128M</span></span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">           <span class="token directive"><span class="token keyword">listen</span> <span class="token number">80</span></span><span class="token punctuation">;</span> <span class="token comment">## listen for ipv4</span></span>
<span class="line">           <span class="token comment">#listen [::]:80 default_server ipv6only=on; ## listen for ipv6</span></span>
<span class="line"></span>
<span class="line">           <span class="token directive"><span class="token keyword">server_name</span> backend.test</span><span class="token punctuation">;</span></span>
<span class="line">           <span class="token directive"><span class="token keyword">root</span>        /path/to/yii-application/backend/web/</span><span class="token punctuation">;</span></span>
<span class="line">           <span class="token directive"><span class="token keyword">index</span>       index.php</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">           <span class="token directive"><span class="token keyword">access_log</span>  /path/to/yii-application/log/backend-access.log</span><span class="token punctuation">;</span></span>
<span class="line">           <span class="token directive"><span class="token keyword">error_log</span>   /path/to/yii-application/log/backend-error.log</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">           <span class="token directive"><span class="token keyword">location</span> /</span> <span class="token punctuation">{</span></span>
<span class="line">               <span class="token comment"># Redirect everything that isn't a real file to index.php</span></span>
<span class="line">               <span class="token directive"><span class="token keyword">try_files</span> <span class="token variable">$uri</span> <span class="token variable">$uri</span>/ /index.php<span class="token variable">$is_args</span><span class="token variable">$args</span></span><span class="token punctuation">;</span></span>
<span class="line">           <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">           <span class="token comment"># uncomment to avoid processing of calls to non-existing static files by Yii</span></span>
<span class="line">           <span class="token comment">#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {</span></span>
<span class="line">           <span class="token comment">#    try_files $uri =404;</span></span>
<span class="line">           <span class="token comment">#}</span></span>
<span class="line">           <span class="token comment">#error_page 404 /404.html;</span></span>
<span class="line"></span>
<span class="line">           <span class="token comment"># deny accessing php files for the /assets directory</span></span>
<span class="line">           <span class="token directive"><span class="token keyword">location</span> ~ ^/assets/.*\.php$</span> <span class="token punctuation">{</span></span>
<span class="line">               <span class="token directive"><span class="token keyword">deny</span> all</span><span class="token punctuation">;</span></span>
<span class="line">           <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">           <span class="token directive"><span class="token keyword">location</span> ~ \.php$</span> <span class="token punctuation">{</span></span>
<span class="line">               <span class="token directive"><span class="token keyword">include</span> fastcgi_params</span><span class="token punctuation">;</span></span>
<span class="line">               <span class="token directive"><span class="token keyword">fastcgi_param</span> SCRIPT_FILENAME <span class="token variable">$document_root</span><span class="token variable">$fastcgi_script_name</span></span><span class="token punctuation">;</span></span>
<span class="line">               <span class="token directive"><span class="token keyword">fastcgi_pass</span> 127.0.0.1:9000</span><span class="token punctuation">;</span></span>
<span class="line">               <span class="token comment">#fastcgi_pass unix:/var/run/php5-fpm.sock;</span></span>
<span class="line">               <span class="token directive"><span class="token keyword">try_files</span> <span class="token variable">$uri</span> =404</span><span class="token punctuation">;</span></span>
<span class="line">           <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">           <span class="token directive"><span class="token keyword">location</span> ~* /\.</span> <span class="token punctuation">{</span></span>
<span class="line">               <span class="token directive"><span class="token keyword">deny</span> all</span><span class="token punctuation">;</span></span>
<span class="line">           <span class="token punctuation">}</span></span>
<span class="line">       <span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><ol start="5">
<li>
<p>更改主机文件以将域指向您的服务器。</p>
<ul>
<li>Windows: <code v-pre>c:\Windows\System32\Drivers\etc\hosts</code></li>
<li>Linux: <code v-pre>/etc/hosts</code></li>
</ul>
</li>
</ol>
<p>添加以下行：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token number">127.0</span>.0.1   frontend.test</span>
<span class="line"><span class="token number">127.0</span>.0.1   backend.test</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><ol start="6">
<li>
<p>打开您的浏览器，并访问 <a href="http://frontend.test/" target="_blank" rel="noopener noreferrer">frontend.test/</a></p>
</li>
<li>
<p>通过选择前端主页顶部的 Sign Up 菜单选项创建一个用户。</p>
</li>
<li>
<p>提供所请求的凭据，并使用 Signup 按钮完成数据输入。你会收到一条信息：</p>
</li>
</ol>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment"># 谢谢您的注册，请检查您的收件箱，并确认电子邮件</span></span>
<span class="line">Thank you <span class="token keyword">for</span> registration<span class="token operator">.</span> Please check your inbox <span class="token keyword">for</span> verification email<span class="token operator">.</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><ol start="9">
<li>
<p>尽管已经发送了确认邮件，但该邮件器的默认设置仍然会阻止真实邮件的发送。相反，在 <code v-pre>@frontend/runtime/mail</code> 目录中创建一个 eml 格式的文件。可以使用 Outlook 或 Thunderbird 等邮件客户机打开该文件，也可以使用文本编辑器检索用于确认用户创建的 URL。在将该 URL 粘贴到浏览器之前，需要修改该 URL 以删除 <a href="https://zh.wikipedia.org/wiki/Quoted-printable" title="引用的可打印编码" target="_blank" rel="noopener noreferrer">引用的可打印编码</a>。</p>
<p>这可以手动完成，具体如下：</p>
<ul>
<li>删除软换行符 ‘=’ 和换行符，以创建下面的单行</li>
<li>修改 ‘=3D’ 为 ‘=’</li>
<li>在 Mac / Linux 上，将 \r\n 转换为 \n - MIME CRLF 换行符是 “真实的”，应该保留。</li>
</ul>
<p>将此URL粘贴到浏览器选项卡中，以完成User的创建。你会看到这个信息：</p>
</li>
</ol>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">Your email has been confirmed<span class="token operator">!</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><ol start="10">
<li>现在，您将自动登录到前端应用程序。然后可以使用相同的凭据登录到后端应用程序。</li>
</ol>
<blockquote>
<p>注意：如果要在单个域上运行高级模板，则 <code v-pre>/</code> 是前端，而 <code v-pre>/admin</code> 是后端，请参阅 <a href="https://learnku.com/docs/yii-framework/2.0.x/advanced-install/topic-shared-hosting.md" target="_blank" rel="noopener noreferrer">在共享主机上使用高级项目模板</a>。</p>
</blockquote>
<h2 id="使用-vagrant-安装-不推荐" tabindex="-1"><a class="header-anchor" href="#使用-vagrant-安装-不推荐"><span>使用 Vagrant 安装（不推荐）</span></a></h2>
<p>这是最简单的安装方式，但是耗时较长（可能约 20 分钟）。</p>
<p><strong>这种安装方式不需要预先安装的软件（如Web服务器，PHP，MySQL等）</strong> - 只是做下一步！</p>
<h4 id="linux-unix-用户手册" tabindex="-1"><a class="header-anchor" href="#linux-unix-用户手册"><span>Linux/Unix 用户手册</span></a></h4>
<ol>
<li>安装 <a href="https://www.virtualbox.org/wiki/Downloads" target="_blank" rel="noopener noreferrer">VirtualBox</a></li>
<li>安装 <a href="https://www.vagrantup.com/downloads.html" target="_blank" rel="noopener noreferrer">Vagrant</a></li>
<li>创建 GitHub <a href="https://github.com/blog/1509-personal-api-tokens" target="_blank" rel="noopener noreferrer">personal API token</a></li>
<li>准备项目:</li>
</ol>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">git</span> clone https://github.com/yiisoft/yii2-app-advanced.git</span>
<span class="line"><span class="token builtin class-name">cd</span> yii2-app-advanced/vagrant/config</span>
<span class="line"><span class="token function">cp</span> vagrant-local.example.yml vagrant-local.yml</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><ol start="4">
<li>将您的GitHub个人API令牌放置到 <code v-pre>vagrant-local.yml</code></li>
<li>将目录更改为项目根目录：</li>
</ol>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token builtin class-name">cd</span> yii2-app-advanced</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><ol start="5">
<li>执行如下命令：</li>
</ol>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">vagrant plugin <span class="token function">install</span> vagrant-hostmanager</span>
<span class="line">vagrant up</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>等待完成后，在浏览器中访问如下URL即可</p>
<ul>
<li>frontend: <a href="http://y2aa-frontend.test/" target="_blank" rel="noopener noreferrer">y2aa-frontend.test</a></li>
<li>backend: <a href="http://y2aa-backend.test/" target="_blank" rel="noopener noreferrer">y2aa-backend.test</a></li>
</ul>
<h4 id="windows-用户手册" tabindex="-1"><a class="header-anchor" href="#windows-用户手册"><span>Windows 用户手册</span></a></h4>
<ol>
<li>
<p>安装 <a href="https://www.virtualbox.org/wiki/Downloads" target="_blank" rel="noopener noreferrer">VirtualBox</a></p>
</li>
<li>
<p>安装 <a href="https://www.vagrantup.com/downloads.html" target="_blank" rel="noopener noreferrer">Vagrant</a></p>
</li>
<li>
<p>重启电脑</p>
</li>
<li>
<p>创建 GitHub <a href="https://github.com/blog/1509-personal-api-tokens" target="_blank" rel="noopener noreferrer">personal API token</a></p>
</li>
<li>
<p>准备项目:</p>
<ul>
<li>下载 <a href="https://github.com/yiisoft/yii2-app-advanced/archive/master.zip" target="_blank" rel="noopener noreferrer">yii2-app-advanced</a></li>
<li>解压</li>
<li>进入 <code v-pre>yii2-app-advanced-master/vagrant/config</code> 文件夹</li>
<li>重命名 <code v-pre>vagrant-local.example.yml</code> 为 <code v-pre>vagrant-local.yml</code></li>
</ul>
</li>
<li>
<p>将您的GitHub个人API令牌放置到 <code v-pre>vagrant-local.yml</code></p>
</li>
<li>
<p>添加如下代码到 <a href="https://en.wikipedia.org/wiki/Hosts_(file)" target="_blank" rel="noopener noreferrer">hosts 文件</a>:</p>
</li>
</ol>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">   <span class="token number">192.168</span><span class="token number">.83</span><span class="token number">.137</span> y2aa<span class="token operator">-</span>frontend<span class="token operator">.</span>test</span>
<span class="line">   <span class="token number">192.168</span><span class="token number">.83</span><span class="token number">.137</span> y2aa<span class="token operator">-</span>backend<span class="token operator">.</span>test</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><ol start="8">
<li>打开终端 (<code v-pre>cmd.exe</code>), <strong>切换路径至项目根目录</strong> 并且执行如下命令:</li>
</ol>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">   vagrant plugin <span class="token function">install</span> vagrant-hostmanager</span>
<span class="line">   vagrant up</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>(猛击 <a href="http://www.wikihow.com/Change-Directories-in-Command-Prompt" target="_blank" rel="noopener noreferrer">这里</a> 查看如何在命令提示符中更改目录)</p>
<p>等待完成后，在浏览器中访问如下URL即可</p>
<ul>
<li>frontend: <a href="http://y2aa-frontend.test/" target="_blank" rel="noopener noreferrer">y2aa-frontend.test</a></li>
<li>backend: <a href="http://y2aa-backend.test/" target="_blank" rel="noopener noreferrer">y2aa-backend.test</a></li>
</ul>
<h2 id="配置-composer" tabindex="-1"><a class="header-anchor" href="#配置-composer"><span>配置 Composer</span></a></h2>
<p>安装项目模板后，最好调整默认的 <code v-pre>composer.json</code> ，它可以在根目录下找到：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"yiisoft/yii2-app-advanced"</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"description"</span><span class="token operator">:</span> <span class="token string">"Yii 2 Advanced Project Template"</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"keywords"</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"yii2"</span><span class="token punctuation">,</span> <span class="token string">"framework"</span><span class="token punctuation">,</span> <span class="token string">"advanced"</span><span class="token punctuation">,</span> <span class="token string">"project template"</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"homepage"</span><span class="token operator">:</span> <span class="token string">"http://www.yiiframework.com/"</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"project"</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"license"</span><span class="token operator">:</span> <span class="token string">"BSD-3-Clause"</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"support"</span><span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token property">"issues"</span><span class="token operator">:</span> <span class="token string">"https://github.com/yiisoft/yii2/issues?state=open"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"forum"</span><span class="token operator">:</span> <span class="token string">"http://www.yiiframework.com/forum/"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"wiki"</span><span class="token operator">:</span> <span class="token string">"http://www.yiiframework.com/wiki/"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"irc"</span><span class="token operator">:</span> <span class="token string">"irc://irc.freenode.net/yii"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"source"</span><span class="token operator">:</span> <span class="token string">"https://github.com/yiisoft/yii2"</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"minimum-stability"</span><span class="token operator">:</span> <span class="token string">"dev"</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"require"</span><span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token property">"php"</span><span class="token operator">:</span> <span class="token string">">=5.6.0"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"yiisoft/yii2"</span><span class="token operator">:</span> <span class="token string">"~2.0.14"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"yiisoft/yii2-bootstrap4"</span><span class="token operator">:</span> <span class="token string">"~2.0.0"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"yiisoft/yii2-swiftmailer"</span><span class="token operator">:</span> <span class="token string">"~2.0.0 || ~2.1.0"</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"require-dev"</span><span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token property">"yiisoft/yii2-debug"</span><span class="token operator">:</span> <span class="token string">"~2.1.0"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"yiisoft/yii2-gii"</span><span class="token operator">:</span> <span class="token string">"~2.2.0"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"yiisoft/yii2-faker"</span><span class="token operator">:</span> <span class="token string">"~2.0.0"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"codeception/codeception"</span><span class="token operator">:</span> <span class="token string">"^4.0"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"codeception/module-asserts"</span><span class="token operator">:</span> <span class="token string">"^1.0"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"codeception/module-yii2"</span><span class="token operator">:</span> <span class="token string">"^1.0"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"codeception/module-filesystem"</span><span class="token operator">:</span> <span class="token string">"^1.0"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"phpunit/phpunit"</span><span class="token operator">:</span> <span class="token string">"~5.7.27 || ~6.5.5"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"codeception/verify"</span><span class="token operator">:</span> <span class="token string">"~0.5.0 || ~1.1.0"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"symfony/browser-kit"</span><span class="token operator">:</span> <span class="token string">">=2.7 &lt;=4.2.4"</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"config"</span><span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token property">"process-timeout"</span><span class="token operator">:</span> <span class="token number">1800</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"fxp-asset"</span><span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token property">"enabled"</span><span class="token operator">:</span> <span class="token boolean">false</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"repositories"</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">{</span></span>
<span class="line">            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"composer"</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token property">"url"</span><span class="token operator">:</span> <span class="token string">"https://asset-packagist.org"</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>首先，我们要更新基本信息。 更改 <code v-pre>name</code> ，<code v-pre>description</code> ，<code v-pre>keywords</code> ，<code v-pre>homepage</code> 和 <code v-pre>support</code> 来匹配您的项目。</p>
<p>接下来是见证奇迹的时刻. 您可以将您的应用程序需要的更多包添加到 <code v-pre>require</code> 部分。所有这些包都来自 <a href="https://packagist.org/" target="_blank" rel="noopener noreferrer">packagist.org</a> 浏览这里你可以找到更多的实用的免费代码。</p>
<p>在你的 <code v-pre>composer.json</code> 改变之后，你可以运行 <code v-pre>composer update --prefer-dist</code> ，等待程序包下载完成，安装后，就可以使用它们了。 包里面所有的类都会自动加载。</p>
<blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


